﻿using System;
using System.Collections.Generic;
using Tools;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
using Image = UnityEngine.UI.Image;

namespace Component.UI
{
    public class UiButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler, IPointerDownHandler
    {
        [SerializeField] private bool main;
        [SerializeField] private List<Image> partner;

        [SerializeField] private ViewManager.ViewType clickEvent;
        [SerializeField] private Transform follow;
        [SerializeField] private float followTime = 1;
        [SerializeField] private Vector3 turn;
        [SerializeField] private Vector2 size = Vector2.one;

        public delegate void ClickEvent();
        
        public ClickEvent OnClickFinished { get; set; }
        public ClickEvent OnClickStart { get; set; }

        public Transform Follow
        {
            get => follow;
            set => follow = value;
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (main || partner.Count < 1)
            {
                return;
            }
            
            LeanTween.scale(gameObject, Vector3.one * 1.2f, .25f).setDelay(.1f);
            foreach (var image in partner)
            {
                var color = image.color;
                color = new Color(color.r, color.g, color.b, .7f);
                image.color = color;
            }
        }
        
        
        public void OnPointerExit(PointerEventData eventData)
        {
            if (main || partner.Count < 1)
            {
                return;
            }
            LeanTween.scale(gameObject, Vector3.one * 1f, .25f).setDelay(.1f);
            
            foreach (var image in partner)
            {
                var color = image.color;
                color = new Color(color.r, color.g, color.b, 1);
                image.color = color;
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            OnClickStart?.Invoke();
            if (follow == null)
            {
                OnClickFinished.Invoke();
                return;
            }
            CameraTools.CameraFollow(follow.position, followTime, turn, delegate
            {
                ViewManager.GetIntoStartView(clickEvent);
                Task task = new Task( CameraTools.SizeUpdate(size.x,size.y, true));
            }, delegate
            {
                OnClickFinished?.Invoke();
            });
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            
        }

        public void StartGame()
        {
            
        }

    }
    public class ViewEvent : UnityEvent<ViewManager.ViewType>
    {
        
    }
}